Cleanup after sending to avoid memory leak #334
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There is a potential memory leak after sending a request using Server#send() or Client#send():
When a request is sent, the
Request
object and the associatedCompletableFuture
are stored in maps (in classQueue
andPromiseRepository
respectively).While both are cleaned up when a successful confirmation is received from the communication partner, this is not the case if either an error or no confirmation at all is received:
PromiseRepository
but not theQueue
is cleaned up.This pull request tries to solve these problems:
Queue
andPromiseRepository
are cleaned up whenever theCompletableFuture
resolves, whether successful or not.This gives users of the
send()
method the chance to add a timeout to the returnedCompletableFuture
, e.g. by using:server.send(sessionIndex, request).orTimeout(10, TimeUnit.SECONDS)
(refer to ServerTest#send_aMessage_promiseCompletesWithTimeout())
As the returned
CompletableFuture
is identical to one insidesend()
, after a timeout occurs, the future is completed with aTimeoutException
and both maps will be cleaned up.I'm very interested in your feedback!
By the way, thank you very much for your work on this nice and elegant library!
Best regards,
Martin